<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 18-3-7
 * Time: 上午10:09
 */

namespace common\components\tools;


class Curl
{
    /**
     * Get Headers function
     * @param string $url
     * @return array
     */
    public static function getHeaders($url)
    {
        $ch = curl_init($url);
        curl_setopt( $ch, CURLOPT_NOBODY, true );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, false );
        curl_setopt( $ch, CURLOPT_HEADER, false );
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
        curl_setopt( $ch, CURLOPT_MAXREDIRS, 3 );
        curl_exec( $ch );
        $headers = curl_getinfo( $ch );
        curl_close( $ch );

        return $headers;
    }

    /**
     * Download
     * @param string $url
     * @param string $path
     * @return bool || void
     */
    public static function download($url, $path)
    {
        # open file to write
        $fp = fopen ($path, 'w+');
        # start curl
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_URL, $url );
        # set return transfer to false
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, false );
        curl_setopt( $ch, CURLOPT_BINARYTRANSFER, true );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
        # increase timeout to download big file
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
        # write data to local file
        curl_setopt( $ch, CURLOPT_FILE, $fp );
        # execute curl
        curl_exec( $ch );
        # close curl
        curl_close( $ch );
        # close local file
        fclose( $fp );
        if (filesize($path) > 0) return true;
    }

    /**
     * Get Headers function
     * @param string $url
     * @return array
     */
    public static function getHeadersByPostWithoutParams($url)
    {
        $url = 'https://www.baidu.com';
        $ch = curl_init($url);
        curl_setopt( $ch, CURLOPT_NOBODY, true );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, false );
        curl_setopt( $ch, CURLOPT_HEADER, false );
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
        curl_setopt( $ch, CURLOPT_MAXREDIRS, 3 );
//        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
//        curl_setopt($ch, CURLOPT_POST, 1);
//        curl_setopt($ch, CURLOPT_POSTFIELDS, ['x'=>'x']);
        curl_exec( $ch );
        $headers = curl_getinfo( $ch );
        curl_close( $ch );

        return $headers;
    }

    /**
     * postDownloadWithoutParams
     * @param string $url
     * @param string $path
     * @return bool || void
     */
    public static function postDownloadWithoutParams($url, $path)
    {
        # open file to write
        $fp = fopen ($path, 'w+');
        # start curl
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_URL, $url );
        # set return transfer to false
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, false );
        curl_setopt( $ch, CURLOPT_BINARYTRANSFER, true );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
        # increase timeout to download big file
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, ['x'=>'x']);
        # write data to local file
        curl_setopt( $ch, CURLOPT_FILE, $fp );
        # execute curl
        curl_exec( $ch );
        # close curl
        curl_close( $ch );
        # close local file
        fclose( $fp );
        if (filesize($path) > 0) return true;
    }

    public static function post($url, $data, $timeout = 30)
    {
        $ssl = substr($url, 0, 8) == "https://" ? TRUE : FALSE;
        $ch = curl_init();
        $opt = array(
            CURLOPT_URL => $url,
            CURLOPT_POST => 1,
            CURLOPT_HEADER => 0,
            CURLOPT_POSTFIELDS => http_build_query($data),
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_TIMEOUT => $timeout,
        );
        if ($ssl) {
            $opt[CURLOPT_SSL_VERIFYHOST] = FALSE;
            $opt[CURLOPT_SSL_VERIFYPEER] = FALSE;
        }
        curl_setopt_array($ch, $opt);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

    public static function getGetUrl($url, $get_data = [])
    {
        $data = [];
        foreach ($get_data as $k => $v){
            $data[] = $k."=".$v;
        }
        $x = Tools::arr2str($data, '&');
        if ($x){
            $url = $url."?".$x;
        }
        return $url;
    }
}